package com.zone.domain.strategy.service.rule.chain.impl;

import com.zone.domain.acticity.repository.IActivityRepository;
import com.zone.domain.strategy.model.entity.StrategyRuleEntity;
import com.zone.domain.strategy.model.valobj.RaffleRuleModelEnum;
import com.zone.domain.strategy.model.valobj.RuleActionEnum;
import com.zone.domain.strategy.model.valobj.RuleEnums;
import com.zone.domain.strategy.repository.IStrategyRepository;
import com.zone.domain.strategy.service.armory.IStrategyDispatch;
import com.zone.domain.strategy.service.rule.chain.AbstractRaffleRuleFilter;
import com.zone.domain.strategy.service.rule.chain.RaffleRuleFilterChain;
import com.zone.types.exception.BizException;
import com.zone.types.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: zongzi
 * @description: 权重抽奖过滤器
 * @date: 2024/6/2
 */
@Slf4j
@Service
@Order(2)
public class RuleWeightRaffleRuleFilter extends AbstractRaffleRuleFilter {
    @Autowired
    private IStrategyRepository strategyRepository;
    @Autowired
    private IStrategyDispatch strategyDispatch;
    @Autowired
    private IActivityRepository activityRepository;

    @Override
    protected boolean isSupport(List<String> ruleModels) {
        return ruleModels.contains(RaffleRuleModelEnum.RULE_BLACKLIST.getCode());
    }

    @Override
    protected RaffleRuleFilterChain.StrategyAwardVO execute(Long strategyId, String userId) {
        // 获取权重规则配置
        List<StrategyRuleEntity> strategyRuleEntityList = strategyRepository.queryStrategyRules(strategyId, RuleEnums.RuleModelEnum.RULE_WEIGHT.getCode());
        if (CollectionUtils.isEmpty(strategyRuleEntityList)) {
            throw new BizException("未查询到权重规则配置！");
        }
        for (StrategyRuleEntity strategyRuleEntity : strategyRuleEntityList) {
            // 用户积分与奖品id集合
            Map<String, List<Integer>> userScoreAwardTable = strategyRuleEntity.parseRuleWeightValues();
            if (userScoreAwardTable.isEmpty()) {
                continue;
            }
            // 用户日抽奖次数作为用户抽奖积分
            Integer userScore = activityRepository.queryUserDayRaffleCount(userId, strategyId);
            // 校验当前用户积分存在哪个范围内(6000, 5000, 4000)
            Integer userScoreBoundary = userScoreAwardTable.keySet().stream()
                    .map(Integer::valueOf)
                    .sorted(Comparator.reverseOrder())
                    .filter(score -> userScore >= score)
                    .findFirst().orElse(null);
            if (Objects.nonNull(userScoreBoundary)) {
                // 使用权重进行抽奖
                Integer awardId = strategyDispatch.getRandomAwardId(strategyId, String.valueOf(userScoreBoundary));
                return RaffleRuleFilterChain.StrategyAwardVO.builder().ruleAction(RuleActionEnum.INTERCEPT).awardId(awardId).build();
            }
        }
        return RaffleRuleFilterChain.StrategyAwardVO.allow();
    }
}
